<?php

namespace App\Manage;


use App\Models\SiteInfo;
use App\Models\User;
use App\Models\UserAgent;
use App\Models\UserAuth;
use App\Service\Base\Manage;
use Illuminate\Support\Facades\DB;

class UserManage extends Manage
{

    // 基本配置
    public function initialize()
    {
        parent::initialize();

        $this->model = 'user';
    }

    // 列表
    // 添加
    // 修改

    // 修改密码
    public function onPassword()
    {
        $option = $this->option();
        $_id    = $option->getData('id', 0, 'intval');
        $pass   = $option->getData('password', null, 'trim');
        $pass2  = $option->getData('password2', null, 'trim');
        if(empty($_id)){
            return $this->_returnFail('请指定操作记录');
        }
        if(empty($pass)){
            return $this->_returnFail('请填写修改后的密码！');
        }
        if($pass2 != $pass){
            return $this->_returnFail('两次输入密码不相同！');
        }

        $model  = $this->checkModel($this->model);
        $result = $model->onPassword($_id, $pass);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 设置为代理商
    public function toAgent()
    {
        $option = $this->option();
        $_uid   = $option->getData('uid', 0, 'intval');
        if(empty($_uid)){
            return $this->_returnFail('请指定操作用户！');
        }
        // 判断用户是否存在
        $userDb = new User();
        $uInfo  = $userDb->oneFind(['id' => $_uid]);
        if(empty($uInfo)){
            return $this->_returnFail('指定用户不存在或者已经被删除！');
        }
        if($uInfo['is_auth'] != 1){
            return $this->_returnFail('该用户未提交认证信息！');
        }
        if($uInfo['verify'] != 1){
            return $this->_returnFail('该用户认证信息还未通过审核！');
        }
        if($uInfo['is_agent'] || $uInfo['agent_id']){
            return $this->_returnFail('该用户已经是代理商了！');
        }


        // 查询认证信息
        $auth = (new UserAuth())->isCache(true)->oneFind(['uid' => $_uid]);
        if(empty($auth)){
            return $this->_returnFail('该用户认证信息不存在！');
        }

        $data = [
            'uid'          => $_uid,
            'agent_type'   => $auth['auth_type'],
            'agent_name'   => $auth['auth_name'],
            'city'         => $auth['city'],
            'grade'        => $auth['grade'],
            'address'      => $auth['address'],
            'telephone'    => $auth['telephone'],
            'description'  => $auth['description'],
            'license_img'  => $auth['license_img'],
            'school_allow' => $auth['school_allow'],
            'form_time'    => $auth['form_time'],
            'end_time'     => $auth['end_time'],
            'is_pay'       => 1,
            'verify'       => 1,
            //'ctime'        => LARAVEL_TIME,
            //'mtime'        => LARAVEL_TIME,
        ];

        // 执行
        $agentDb = new UserAgent();
        $agentDb->fill($data);
        DB::beginTransaction();
        try{
            // 复制用户认证信息到代理商信息中
            $agentDb->save();

            // 更新用户代理商ID
            $upData = ['is_agent' => 1, 'agent_id' => $agentDb->id, 'verify_agent' => 1];
            $userDb->where('id', $_uid)->update($upData);

            DB::commit();
        } catch(\Exception $e){
            DB::rollBack();

            return $this->_returnFail($e->getMessage());
        }

        // 删除缓存
        $agentDb->deleteCacheShow($data);
        $userDb->deleteCacheShow($upData, ['id' => $_uid]);

        return true;
    }

    // 软删除
    protected function toSoft2()
    {
        $_id = $this->id();
        if(empty($_id)){
            return $this->_returnFail('请指定删除记录');
        }

        $where = $this->delWhere();
        if($where === false){
            return $this->_returnFail($this->error);
        }

        $model  = $this->checkModel($this->model);
        $result = $model->isCache(true)->oneEdit($where, ['status' => -1]);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }


    // 软删除
    protected function toSoft()
    {
        $_uid = $this->option()->getData('uid', 0, 'intval');
        if(empty($_uid)){
            return $this->_returnFail('请指定删除记录');
        }

        $where = $this->delWhere();
        if($where === false){
            return $this->_returnFail($this->error);
        }

        $model  = $this->checkModel($this->model);
        $result = $model->isCache(true)->cancel($_uid);
        if($result === false){
            return $this->_returnFail($model->getError());
        }

        return $result;
    }

    // 列表查询
    protected function listWhere()
    {
        $option   = $this->option();
        $where    = [['status', '>=', 0]];
        $bang     = [];
        $c_id     = $option->getData('course_id', 0, 'intval');
        $uid      = $option->getData('uid', 0, 'intval');
        $mobile   = $option->getData('mobile', null, 'trim');
        $nickname = $option->getData('nickname', null, 'trim');
        $keyword  = $option->getData('keyword', null, 'trim');
        if($c_id){
            $where[] = ['course_id', '=', $c_id];
            $bang    = ['course_id'];
        }
        if($uid){
            $where[] = ['uid', '=', $uid];
            $bang    = ['uid'];
        }
        if($nickname){
            $where[] = ['nickname', 'like', "%".$nickname."%"];
            $bang    = false;
        }
        if($mobile){
            $where[] = ['mobile', 'like', "%".$mobile."%"];
            $bang    = false;
        }
        if($keyword){
            $bang = false;
            //$where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
        }

        // 缓存
        $this->bang = $bang;

        return $where;
    }

    // 添加之前
    protected function _before_add()
    {
        $opt  = $this->option();
        $data = [
            //'id'        => $opt->getData("id", null, "intval"),
            //'score'     => $opt->getData("score", null, "intval"),
            'username' => $opt->getData("username", null, "trim"),
            'password' => $opt->getData("password", null, "trim"),
            'nickname' => $opt->getData("nickname", null, "trim"),
            'head_img' => $opt->getData("head_img", null, "trim"),
            'mobile'   => $opt->getData("mobile", null, "trim"),
            'email'    => $opt->getData("email", null, "trim"),
            'sex'      => $opt->getData("sex", null, "intval"),
            'city'     => $opt->getData("city", null, "trim"),
            //'reg_ip'    => $opt->getData("reg_ip", null, "trim"),
            //'last_ip'   => $opt->getData("last_ip", null, "trim"),
            //'last_time' => $opt->getData("last_time", null, "intval"),
            'sign'     => $opt->getData("sign", null, "trim"),
            'status'   => $opt->getData("status", null, "intval"),
            'openid'   => $opt->getData("openid", null, "trim"),
            'from_uid' => $opt->getData("from_uid", null, "intval"),
            //'money'     => $opt->getData("money", null, "trim"),
            //'is_auth'   => $opt->getData("is_auth", null, "intval"),
            //'auth_type' => $opt->getData("auth_type", null, "trim"),
            //'auth_name' => $opt->getData("auth_name", null, "trim"),
            //'auth_time' => $opt->getData("auth_time", null, "intval"),
            //'auth_id'   => $opt->getData("auth_id", null, "trim"),
            //'end_time'  => $opt->getData("end_time", null, "intval"),
            //'verify'    => $opt->getData("verify", null, "intval"),
            //'ctime'     => $opt->getData("ctime", null, "intval"),
            //'mtime'     => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt  = $this->option();
        $data = [
            //'id'        => $opt->getData("id", null, "intval"),
            //'score'     => $opt->getData("score", null, "intval"),
            'username' => $opt->getData("username", null, "trim"),
            'password' => $opt->getData("password", null, "trim"),
            'nickname' => $opt->getData("nickname", null, "trim"),
            'head_img' => $opt->getData("head_img", null, "trim"),
            'mobile'   => $opt->getData("mobile", null, "trim"),
            'email'    => $opt->getData("email", null, "trim"),
            'sex'      => $opt->getData("sex", null, "intval"),
            'city'     => $opt->getData("city", null, "trim"),
            //'reg_ip'    => $opt->getData("reg_ip", null, "trim"),
            //'last_ip'   => $opt->getData("last_ip", null, "trim"),
            //'last_time' => $opt->getData("last_time", null, "intval"),
            'sign'     => $opt->getData("sign", null, "trim"),
            'status'   => $opt->getData("status", null, "intval"),
            'openid'   => $opt->getData("openid", null, "trim"),
            'from_uid' => $opt->getData("from_uid", null, "intval"),
            //'money'     => $opt->getData("money", null, "trim"),
            //'is_auth'   => $opt->getData("is_auth", null, "intval"),
            //'auth_type' => $opt->getData("auth_type", null, "trim"),
            //'auth_name' => $opt->getData("auth_name", null, "trim"),
            //'auth_time' => $opt->getData("auth_time", null, "intval"),
            //'auth_id'   => $opt->getData("auth_id", null, "trim"),
            //'end_time'  => $opt->getData("end_time", null, "intval"),
            //'verify'    => $opt->getData("verify", null, "intval"),
            //'ctime'     => $opt->getData("ctime", null, "intval"),
            //'mtime'     => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

}


